Intermediate / 1 to 5 years experienced level questions
Intermediate / 1 to 5 years experienced level questions & answers
Ques 1. Explain Code First approach in Entity Framework.
Code First is an approach where you define your data model using C# or VB.NET classes, and the database is generated based on these classes.
Ques 2. Differentiate between DbSet and DbContext in Entity Framework.
DbContext is a representation of a session with the database, while DbSet is a collection of entities that are used to perform database operations on those entities.
Ques 3. Explain Lazy Loading in Entity Framework.
Lazy Loading is a feature in EF where related entities are not loaded from the database until they are explicitly accessed in code.
Ques 4. What is Eager Loading in Entity Framework?
Eager Loading is the opposite of Lazy Loading. It loads the related entities along with the main entity in a single database query.
Ques 5. What is the Database First approach in Entity Framework?
Database First is an approach where the data model is generated from an existing database, and the corresponding classes are created in the application.
Ques 6. How can you execute raw SQL queries in Entity Framework?
You can use the SqlQuery method of the Database property in DbContext to execute raw SQL queries.
Example:
var result = context.Database.SqlQuery("SELECT * FROM MyTable");
Ques 7. Explain the concept of migrations in Entity Framework.
Migrations are a way to manage database schema changes over time. They allow you to evolve your database schema as your application evolves.
Ques 8. Explain the purpose of the EntityState enumeration in Entity Framework.
EntityState is an enumeration that represents the state of an entity being tracked by the DbContext. It can be used to determine whether an entity is new, modified, or deleted.
Ques 9. What is the purpose of the DatabaseGenerated attribute in Entity Framework?
The DatabaseGenerated attribute is used to configure how values for a property are generated by the database when inserting a new record.
Example:
[DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int Id { get; set; }
Ques 10. What is the purpose of the ExecuteSqlCommand method in Entity Framework?
The ExecuteSqlCommand method is used to execute a SQL command that does not return any results, such as an UPDATE or DELETE statement.
Example:
context.Database.ExecuteSqlCommand("UPDATE MyTable SET Column1 = 1 WHERE Id = 1");
Ques 11. Explain the purpose of the IDbSet interface in Entity Framework.
The IDbSet interface is an interface that represents a collection of entities in the context. It is implemented by DbSet.
Ques 12. How can you execute stored procedures in Entity Framework?
You can execute stored procedures in EF using the context.Database.SqlQuery method or by creating a method on your DbContext that calls SqlQuery or ExecuteSqlCommand.
Ques 13. Explain the purpose of the ChangeTracker in Entity Framework.
ChangeTracker is a property of DbContext that keeps track of the changes made to entities, allowing you to inspect and revert changes before saving to the database.
Ques 14. Explain the purpose of the [ForeignKey] attribute in Entity Framework.
The [ForeignKey] attribute is used to specify the foreign key property in a relationship when the default convention is not followed.
Example:
[ForeignKey("CategoryId")] public int CategoryId { get; set; }
Ques 15. What is the purpose of the Entry method in Entity Framework?
The Entry method is used to access the EntityEntry object, which provides information about an entity being tracked by the context and allows you to perform actions like updating and deleting.
Example:
var entry = context.Entry(entity);
Ques 16. Explain the purpose of the State property in Entity Framework.
The State property of EntityEntry is used to get or set the state of an entity being tracked by the context, such as Added, Modified, or Deleted.
Example:
entry.State = EntityState.Modified;
Ques 17. What is the purpose of the FromSqlRaw method in Entity Framework?
The FromSqlRaw method is used to create a query based on raw SQL and map the result to entities. It is often used for executing stored procedures.
Example:
var result = context.Entities.FromSqlRaw("EXEC dbo.GetEntities");
Ques 18. Explain the purpose of the IsDeleted property in Entity Framework.
The IsDeleted property is often used in soft delete scenarios where instead of actually deleting a record, a flag is set to mark it as deleted.
Example:
public bool IsDeleted { get; set; }
Ques 19. What is the purpose of the Attach method in Entity Framework?
The Attach method is used to reattach a disconnected entity to the context, allowing you to update it without having to retrieve it again from the database.
Example:
context.Entities.Attach(disconnectedEntity);
Most helpful rated by users: